import payOrderAjax from '@/interface/order/payOrder'
import h5PayOrderAjax from '@/interface/order/h5PayOrder'
import dialog from '@/utils/dialog'

const transOpt = (opt) => {
  const query = {};
  if(opt.subOrderNo){
    query.subOrderNo = opt.subOrderNo
  }else{
    query.orderNo = opt.orderNo
  }
  return query
}

const winxinH5Pay = (opt) => {
  const query = transOpt(opt)
  h5PayOrderAjax(query).then(data => {
    const url = encodeURIComponent(`http://h5app.homecover.cn/#/pages/status/main?status=confirmPay`)
    window.location.href = `${data}&redirect_url=${url}`;
  }).catch(msg => {
    dialog.showToast(msg || '获取支付信息失败')
  })
}
const weixinXCXPay = (opt, successFun, cancelFun) => {
  const query = transOpt(opt)
  uni.login({
    success: res => {
      payOrderAjax({
        ...query,
        code: res.code
      }).then(data => {
        uni.requestPayment({
          ...data,
          success: () => {
            successFun && successFun()
          },
          fail: () => {
            cancelFun && cancelFun()
          }
        })
      }).catch(msg => {
        dialog.showToast(msg || '获取支付信息失败')
      })
    }
  })
}
export default function weixinPay(params, successFun, cancelFun) {
  let fun = winxinH5Pay;
  // #ifdef  MP-WEIXIN
  fun = weixinXCXPay
  // #endif
  fun(params, successFun, cancelFun)
}
